package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.DemandAnalysis;
import com.crm.model.entity.customerManager.DemandResearch;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.DemandAnalysisDbService;
import com.crm.service.opportunityManager.DemandResearchDbService;
import com.crm.system.service.opportunityManager.DemandAnalysisService;
import com.crm.system.service.opportunityManager.DemandResearchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 需求调研
 */
@Service
public class DemandResearchServiceImpl implements DemandResearchService {

    @Autowired
    private DemandResearchDbService demandResearchDbService;

    /**
     * 商机-商机详情-需求调研
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<DemandResearch> demandResearchList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<DemandResearch> list = demandResearchDbService.demandResearchList(opportunityId,"create_tm desc");
        PageInfo<DemandResearch> pageInfo = new PageInfo<DemandResearch>(list);
        return new DhPageInfo<DemandResearch>(pageInfo, list);
    }

    /**
     * 商机管理-商机详情-新增需求调研
     * @param demandResearch 需求调研实体
     * @return
     */
    @Override
    public boolean addDemandResearch(DemandResearch demandResearch) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String demandResearchId = CommonUtils.getGUID();
        demandResearch.setDemandResearchId(demandResearchId);
        demandResearch.setCreatePeopleId(userAccount.getAccountId());
        demandResearch.setCreateTm(new Date());
        demandResearch.setUpdatePeopleId(userAccount.getAccountId());
        demandResearch.setUpdateTm(new Date());
        demandResearch.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        Boolean result = demandResearchDbService.addDemandResearch(demandResearch);
        return result;
    }

    /**
     * 商机-商机详情-修改需求调研
     * @param demandResearchId 需求调研id
     * @return
     */
    @Override
    public DemandResearch getDemandResearchById(String demandResearchId) {
        return demandResearchDbService.getDemandResearchById(demandResearchId);
    }

    /**
     * 商机详情-修改需求调研
     * @param demandResearch 需求调研实体
     * @return
     */
    @Override
    public Boolean updateDemandResearch(DemandResearch demandResearch) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        demandResearch.setUpdatePeopleId(userAccount.getAccountId());
        demandResearch.setUpdateTm(new Date());
        Boolean result = demandResearchDbService.updateDemandResearch(demandResearch);
        return result;
    }

    /**
     * 商机详情-删除需求调研
     * @param demandResearch 需求调研实体
     * @return
     */
    @Override
    public Boolean deleteDemandResearch(DemandResearch demandResearch) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        demandResearch.setUpdatePeopleId(userAccount.getAccountId());
        demandResearch.setUpdateTm(new Date());
        demandResearch.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return demandResearchDbService.updateDemandResearch(demandResearch);
    }
}
